package order;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cims.CustomerRecord;

public class CustomerServlet extends HttpServlet {

	/* (非 Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		perform(req, resp);
	}

	/* (非 Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		perform(req, resp);
	}

	private void perform(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");

		String number = req.getParameter("number");

		CustomerRecord crec = new CustomerRecord();
		Map<String, String> map = crec.getInfo(number);

		String path = null;
		if (map == null) {
			req.setAttribute("msg", "顧客番号に誤りがあります。");
			path = "/order/customer_entry.jsp";
		}
		else {
			req.setAttribute("customer", new Customer(map))	;
			path = "/order/order_entry.jsp";
		}

		RequestDispatcher disp = req.getRequestDispatcher(path);
		disp.forward(req, resp);
	}
}
